/*
    一个工具函数模块，封装项目中常用的一些函数
*/
;
(function(root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(factory);
    } else {
        root.legendofkage = root.legendofkage || {};
        root.legendofkage.Util = factory();
    }
})(this, function() {
    var Util = function() {};

    //清除对象的所有属性
    Util.clear = function(obj) {
        for (var property in obj) {
            delete obj[property];
        }
    }

    //实现继承,subClass继承parentClass
    Util.inherits = function(subClass, parentClass) {
        var obj = Object.create(parentClass.prototype);
        subClass.prototype = obj;
        subClass.prototype.constructor = subClass;
        subClass.__super__ = parentClass.prototype;
    }

    //将编码的html特殊字符还原为标记
    Util.htmlDecode = function(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&lt;/g, "<");
        s = s.replace(/&gt;/g, ">");
        s = s.replace(/&nbsp;/g, " ");
        s = s.replace(/&#39;/g, "\'");
        s = s.replace(/&quot;/g, "\"");
        s = s.replace(/<br>/g, "\n");
        return s;
    }


    //ajax简易实现，类似于jquery.ajax传参
    Util.ajax = function(_opt) {
        var opt = Object.assign({
            contentType: 'application/x-www-form-urlencoded',
            async: true
        }, (_opt || {}));
        var xmlHttp;
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        } else {
            var msxmls = new Array(
                'Msxml2.XMLHTTP.5.0',
                'Msxml2.XMLHTTP.4.0',
                'Msxml2.XMLHTTP.3.0',
                'Msxml2.XMLHTTP',
                'Microsoft.XMLHTTP');
            for (var i = 0; i < msxmls.length; i++) {
                try {
                    xmlHttp = new ActiveXObject(msxmls[i]);
                    break;
                } catch (e) {}
            }
        }

        function getResponseText() {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    opt.success(this.responseText);
                }
            }
        }
        //根据浏览器版本的不同,XMLHttpRequest可能属于Level 1,也可能属于Level 2,其api可能有所不同
        "onload" in xmlHttp && (xmlHttp.onload = getResponseText);
        "onreadystatechange" in xmlHttp && (xmlHttp.onreadystatechange = getResponseText);
        xmlHttp.open(opt.type, opt.url, opt.async);
        xmlHttp.setRequestHeader("Content-Type", opt.contentType);
        xmlHttp.send(data);
    }

    //对象合并(将target中的属性添加到source上，如果source上存在同名属性，则覆盖其值.当deepCopy为真时，则深度复制)
    Util.extend = function(deepCopy, source, target) {
        if (!deepCopy) {
            for (var item in target) {
                source[item] = target[item];
            }
        } else {
            for (var item in target) {
                if (!source.hasOwnProperty(item)) {
                    source[item] = target[item];
                } else if (typeof target[item] != "object") {
                    source[item] = target[item];
                } else {
                    arguments.callee(deepCopy, source[item], target[item]);
                }
            }
        }
    }


    Util.map = {
        ol: {
            /**
             * 获取一个点跑另一个点的方向
             * 
             * @param {any} curPos 当前投影坐标
             * @param {any} targetPos 目标投影坐标
             * @returns 
             */
            getRotation: function(curPos, targetPos) {
                var deg = 0;
                if (targetPos[0] != curPos[0]) {
                    deg = Math.atan2(targetPos[1] - curPos[1], targetPos[0] - curPos[0]);
                    deg = 180 * deg / Math.PI;
                }
                return deg;
            }
        }
    }

    Object.assign = (typeof Object.assign === "function") ? Object.assign : function(target) {
        target = Object(target);
        for (var index = 1; index < arguments.length; index++) {
            var source = arguments[index];
            if (source != null) {
                for (var key in source) {
                    if (Object.prototype.hasOwnProperty.call(source, key)) {
                        target[key] = source[key];
                    }
                }
            }
        }
        return target;
    };

    return Util;
});